package dev.tools.exchange;

import io.vertx.core.Vertx;
import io.vertx.ext.web.client.WebClient;

import java.util.concurrent.CompletableFuture;

public class Client {

    private int port;

    public Client(int port) {
        this.port = port;
    }

    public String send(String host, String code) throws Exception {
        Vertx vertx = Vertx.vertx();
        WebClient webClient = WebClient.create(vertx);

        CompletableFuture<String> completableFuture = new CompletableFuture<>();

        webClient.postAbs("http://" + host + ":" + port + "/source")
                .addQueryParam("code", code)
                .send(handle -> {
                    if (handle.succeeded()) {
                        completableFuture.complete(handle.result().bodyAsString());
                    } else {
                        completableFuture.completeExceptionally(handle.cause());
                    }
                });
        return completableFuture.get();
    }
}